<?php

namespace App\Kuafu\Mybank\Controller\B;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Export\Model\ExportDownloadModel;
use App\Kuafu\Export\Service\ExportDownloadService;
use App\Kuafu\Mybank\Service\MybankService;
use App\Kuafu\Order\Enum\OrderMerchantEnum;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class MybankController extends BaseController
{

    /**
     * 总账户入账记录
     * @param Request $request
     * @return array|JsonResponse
     */
    public function totalBillList(Request $request)
    {
        $pageSize  = (int)$request->get('pageSize', 10);
        $pageNum   = (int)$request->get('pageNum', 1);
        $export    = (bool)$request->get("export", false);
        $startDate = (string)$request->get('startDate', '');   //开始时间
        $endDate   = (string)$request->get('endDate', '');     //结束时间

        $data = MybankService::totalBillList($startDate, $endDate, $pageSize, $pageNum, $export);
        return $this->success($data);
    }

    /**
     * 后台商户收支明细
     * @param Request $request
     * @return array|JsonResponse
     */
    public function expenseRecordList(Request $request)
    {
        $pageSize     = (int)$request->get('pageSize', 10);
        $pageNum      = (int)$request->get('pageNum', 1);
        $export       = (bool)$request->get("export", false);
        $type         = (int)$request->get('type', 0);            //转账类型 0 未知 1 提现支出 2 其他支出 3 收入
        $startDate    = (string)$request->get('startDate', date("Y-m-d", strtotime('-3 months')));   //开始时间
        $endDate      = (string)$request->get('endDate', date("Y-m-d"));     //结束时间
        $source       = (string)$request->get('source', '');          //来源 1美团 2 饿了么
        $merchantName = (string)$request->get('merchantName', '');//商户名
        $merchantId   = (string)$request->get('merchantId', '');  //网商的商户号
        $shopName     = (string)$request->get('shopId', '');
        $queryType    = (int)$request->get('queryType', 0);

        if (strtotime($startDate) > strtotime($endDate)) {
            return $this->fail(BaseCodeEnum::ERR_400000, '时间错误');
        }
        // 时间限制： 仅支持三个月跨度的数据查询与导出
        if ((strtotime($endDate) - strtotime($startDate)) > 86400 * 93) {
            return $this->fail(BaseCodeEnum::ERR_400000, '最大支持三个月区间范围');
        }

        if ($export) {
            $params = [
                'type'         => $type,
                'source'       => $source,
                'merchantName' => $merchantName,
                'merchantId'   => $merchantId,
                'startDate'    => $startDate,
                'endDate'      => $endDate,
                'shopName'     => $shopName,
                'pageSize'     => $pageSize,
                'pageNum'      => $pageNum,
                'export'       => $export,
                'queryType'    => $queryType
            ];

            $completeTimeStart = strtotime($startDate);
            $completeTimeEnd   = strtotime($endDate);
            ExportDownloadService::export(ExportDownloadModel::CLIENT_TYPE_WEB,
                ExportDownloadModel::FILE_TYPE_WEB_MYBANK_RECORD, 0,
                OrderMerchantEnum::SHOW_TIME_TAB_DEFINE, $completeTimeStart, $completeTimeEnd, [], $params);
            return $this->success();
        }
        $data = MybankService::expenseRecordList($type, $source, $merchantName, $merchantId, $startDate, $endDate,
            $shopName, $pageSize, $pageNum, $export, 0);
        return $this->success($data);
    }

    /**
     * 后台财务清分列表
     * @param Request $request
     * @return array|JsonResponse
     */
    public function financeList(Request $request)
    {
        $pageSize      = (int)$request->get('pageSize', 20);
        $pageNum       = (int)$request->get('pageNum', 1);
        $export        = (bool)$request->get("export", false);
        $startDate     = (string)$request->get('startDate', '');    //入账日期-开始时间
        $endDate       = (string)$request->get('endDate', '');      //入账日期-结束时间
        $billStartDate = (string)$request->get('billStartDate', '');//归属账期-开始时间
        $billEndDate   = (string)$request->get('billEndDate', '');  //归属账期-结束时间
        $source        = (int)$request->get('source', 0);           //来源 1美团 2 饿了么
        $merchantName  = (string)$request->get('merchantName', ''); //商户名
        $merchantId    = (string)$request->get('merchantId', '');   //网商的商户号
        $type          = (string)$request->get('transType', '');   //转账类型 0 未知 1 提现支出 2 其他支出 3 收入
        $shopName      = (string)$request->get('shopName', '');
        $flagDivision  = (int)$request->get('flagDivision', -1);
        $vostroBillId  = (int)$request->get('vostroBillId', 0);//网商银行来账记录 ID

        $data = MybankService::financeList($startDate, $endDate, $billStartDate, $billEndDate, $source,
            $merchantName, $merchantId, $shopName, $flagDivision, $type, $pageSize, $pageNum, $export, $vostroBillId
        );
        return $this->success($data);
    }

    /**
     * 后台财务清分明细
     * @param Request $request
     * @return array|JsonResponse
     */
    public function financeDetail(Request $request)
    {
        $pageSize      = (int)$request->get('pageSize', 10);
        $pageNum       = (int)$request->get('pageNum', 1);
        $export        = (bool)$request->get("export", false);
        $startDate     = (string)$request->get('startDate', '');    //入账日期-开始时间
        $endDate       = (string)$request->get('endDate', '');      //入账日期-结束时间
        $billStartDate = (string)$request->get('billStartDate', '');//归属账期-开始时间
        $billEndDate   = (string)$request->get('billEndDate', '');  //归属账期-结束时间
        $source        = (int)$request->get('source', 0);           //来源 1美团 2 饿了么
        $merchantName  = (string)$request->get('merchantName', ''); //商户名
        $merchantId    = (string)$request->get('merchantId', '');   //网商的商户号
        $shopName      = (string)$request->get('shopName', '');
        $billRecordId  = (int)$request->get('billRecordId', 0);//账单记录 ID

        $data = MybankService::financeDetail($startDate, $endDate, $billStartDate, $billEndDate, $source,
            $merchantName, $merchantId, $shopName, $pageSize, $pageNum, $export, $billRecordId
        );
        return $this->success($data);
    }

    /**
     * 批次查询接口
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function batchQuery(Request $request): JsonResponse
    {
        $batchNo         = (string)$request->get('batchNo', '');
        $batchInstanceId = (string)$request->get('batchInstanceId', '');

        $data = MybankService::batchQuery($batchNo, $batchInstanceId);
        return $this->success($data);
    }
}
